using System.Collections.Generic;

namespace StackCalc.Instructions
{
	public class Apply : IInstruction
	{
		public void Execute(List<IInstruction> stack, StackEngine stackEngine)
		{
			// pop argument
			BracedExpression argument = stack.PeekInstructionFromTop(1) as BracedExpression;

			if (argument != null)
			{
				// remove braced expression
				stack.PopInstructionFromTop(1);

				// add content to input
				stackEngine.InvokeApplyEvent(new EventArgs<string>(argument.StoredValue));
			}
		}

		public string Validate(List<IInstruction> stack)
		{
			if (stack.Count == 0)
			{
				return "Not enough elements on stack!";
			}

			return string.Empty;
		}
	}
}